import Vue from 'vue'
import localStorageService from 'store/localStorageService.js'

const state = {
  nowStore: localStorageService.getNowStore(),
  nowAddressPredix: localStorageService.getNowAddressPredix(),
  shoppingCart: localStorageService.getShoppingCart()
}

const getters = {
  nowStore: state => state.nowStore,
  nowAddressPredix: state => state.nowAddressPredix,
  // shoppingCart: state => {
  //   console.log('here')
  //   let result = []
  //   for (let cartItem in state.shoppingCart) {
  //     result.push(state.shoppingCart[cartItem])
  //   }
  //   return result
  // },
  shoppingCart: state => state.shoppingCart,
  shoppingCartTotalCost: state => {
    let totalCost = 0
    for (let cartItem in state.shoppingCart) {
      totalCost +=
        state.shoppingCart[cartItem].product.price *
        state.shoppingCart[cartItem].amount
    }
    return totalCost
  }
}

const actions = {
  setNowStore({ commit }, nowStore) {
    commit('setNowStore', nowStore)
  },
  setNowAddressPredix({ commit }, nowAddressPredix) {
    commit('setNowAddressPredix', nowAddressPredix)
  },
  addToShoppingCart({ commit }, product) {
    commit('addToShoppingCart', product)
  },
  removeFromShoppingCart({ commit }, product) {
    commit('removeFromShoppingCart', product)
  },
  removeOneFromShoppingCart({ commit }, product) {
    commit('removeOneFromShoppingCart', product)
  },
  clearShoppingCart({ commit }) {
    commit('clearShoppingCart')
  }
}

const mutations = {
  setNowStore(state, nowStore) {
    state.nowStore = nowStore
    localStorageService.setNowStore(nowStore)
  },
  setNowAddressPredix(state, nowAddressPredix) {
    state.nowAddressPredix = nowAddressPredix
    localStorageService.setNowAddressPredix(nowAddressPredix)
  },
  addToShoppingCart(state, product) {
    let cartItem = {}
    let amount = 0
    if (state.shoppingCart === null) {
      state.shoppingCart = {}
    } else {
      if (state.shoppingCart[product.id]) {
        amount = state.shoppingCart[product.id].amount
      }
    }
    cartItem = {
      product: product,
      amount: amount + 1
    }
    Vue.set(state.shoppingCart, product.id, cartItem)
    localStorageService.setShoppingCart(state.shoppingCart)
  },
  removeFromShoppingCart(state, product) {
    if (!state.shoppingCart[product.id]) {
      return
    } else {
      Vue.delete(state.shoppingCart, product.id)
    }
    localStorageService.setShoppingCart(state.shoppingCart)
  },
  removeOneFromShoppingCart(state, product) {
    let amount = 0
    if (!state.shoppingCart[product.id]) {
      return
    } else {
      amount = state.shoppingCart[product.id].amount
    }
    amount = amount - 1
    if (amount <= 0) {
      Vue.delete(state.shoppingCart, product.id)
    } else {
      Vue.set(state.shoppingCart, product.id, {
        product: product,
        amount: amount
      })
    }
    localStorageService.setShoppingCart(state.shoppingCart)
  },
  clearShoppingCart(state) {
    state.shoppingCart = {}
    localStorageService.setShoppingCart(null)
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
